package net.youball.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.youball.conf.entity.Location;
import net.youball.conf.entity.XY;
import net.youball.entity.BallUser;
import net.youball.service.IFriendService;
import net.youball.utils.Json;
import net.youball.utils.LocationUtils;
import net.youball.utils.StringUtils;



public class FriendWebServiceImpl implements IFriendWebService {
	
	public IFriendService friendService;
	public String example(String message) {
		return message;
	}
	public String addLocation(String args){
		Map<String,Object> map=Json.jsonToObject(args);
		String x=map.get("x").toString();
		String y=map.get("y").toString();
		if(StringUtils.compareTrim(x,y)&&StringUtils.compareNumber(x,y)){
			XY xy=new XY();
			xy.setX(LocationUtils.getXYFirst(x));
			xy.setY(LocationUtils.getXYFirst(y));
			//ServletContext app= XFireServletController.getRequest().getSession().getServletContext();
			Location l=new Location(LocationUtils.getXYLast(x),LocationUtils.getXYLast(y));
			LocationUtils.setLocation(xy, l);
		}
		return null;
	}
	
	public String findNearby(String args) {
		// TODO Auto-generated method stub
		Map<String,Object> map=Json.jsonToObject(args);
		String x=map.get("x").toString();
		String y=map.get("y").toString();
		Map<String,String> info=new HashMap<String, String>();
		if(StringUtils.compareTrim(x,y)&&StringUtils.compareNumber(x,y)){
			XY xy=new XY();
			xy.setX(LocationUtils.getXYFirst(x));
			xy.setY(LocationUtils.getXYFirst(y));
			//ServletContext app= XFireServletController.getRequest().getSession().getServletContext();
			//Location l=new Location(LocationUtils.getXYLast(x),LocationUtils.getXYLast(y));
			//LocationUtils.setLocation(xy, l);
			List<Location> list=LocationUtils.getLocation(xy);
			
			for(int i=0;i<list.size();i++){
				BallUser user=list.get(i).getUser();
				map.put(user.getUserId().toString(), user.getNickName());
			}
			
		}
		if(map!=null){
			return Json.objectToStr(info);
		}
		
		return "";
	}
	public IFriendService getFriendService() {
		return friendService;
	}
	public void setFriendService(IFriendService friendService) {
		this.friendService = friendService;
	}
	public String findAll() {
		// TODO Auto-generated method stub
		//ServletContext app= XFireServletController.getRequest().getSession().getServletContext();
		//Map<XY,List<Location>> map=(Map<XY, List<Location>>) app.getAttribute("location");
		//System.out.println(map);
		//return map.toString();
		return null;
	}
	
	
}